package com.building.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.building.common.R;
import com.building.enums.REnum;
import com.building.service.impl.BaseServiceImpl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import com.building.mapper.ShoppingMapper;
import com.building.entity.Shopping;
import com.building.service.ShoppingService;
/**
@author yinjiahui
@create 2021-04-11 17:11
*/
@Service
public class ShoppingServiceImpl extends BaseServiceImpl<ShoppingMapper, Shopping> implements ShoppingService{

    @Override
    public int updateBatch(List<Shopping> list) {
        return baseMapper.updateBatch(list);
    }
    @Override
    public int updateBatchSelective(List<Shopping> list) {
        return baseMapper.updateBatchSelective(list);
    }
    @Override
    public int batchInsert(List<Shopping> list) {
        return baseMapper.batchInsert(list);
    }
    @Override
    public int insertOrUpdate(Shopping record) {
        return baseMapper.insertOrUpdate(record);
    }
    @Override
    public int insertOrUpdateSelective(Shopping record) {
        return baseMapper.insertOrUpdateSelective(record);
    }

    /**
     *  根据用户id查询购物车中所有物品
     * @param userId
     * @return
     */
    @Override
    public List<Shopping> selectShoppingByUserId(Integer userId) {
        QueryWrapper query = new QueryWrapper();
        query.eq("user_id",userId);
        return this.shoppingMapper.selectList(query);
    }

    /**
     * 根据用户id和商品id删除商品
     * @param userId
     * @param shoppingId
     */
    @Override
    public void deleteShoppingByUserIdAndShoppingId(Integer userId, Integer shoppingId) {
        QueryWrapper delete = new QueryWrapper();
        delete.eq("user_id",userId);
        delete.eq("shopping_id",shoppingId);
        this.shoppingMapper.delete(delete);
    }

    @Override
    public Shopping selectShoppingByShopping(Integer shoppingId) {
        return this.shoppingMapper.selectById(shoppingId);
    }

    /**
     *
     * 新增或修改商品到购物车中
     *
     * @param shopping
     * @return
     */
    @Override
    public R insertOrUpdateShopping(Shopping shopping) {

        if (shopping.getShoppingId() == null){//新增
            this.shoppingMapper.insert(shopping);
            return R.result(REnum.SUCCESS_INSERT);
        }else {
            this.shoppingMapper.updateById(shopping);
            return R.result(REnum.SUCCESS_UPDATE);
        }
    }



}
